package com.orhon.smartcampus.modules.baseinfo.mutation;

import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.baseinfo.dao.WeChatTemplateDao;
import com.orhon.smartcampus.modules.baseinfo.entity.WeChatTemplate;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class DeleteWeChatTemplateMutation implements DataFetcher {


    @Autowired
    WeChatTemplateDao weChatTemplateDao;

    /**
     * 删除微信消息模版
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {
        if (environment.getArgument("templateId") != null) {
            List<String> ids = environment.getArgument("templateId");
            ids.forEach(id -> {
                        WeChatTemplate item = weChatTemplateDao.get(WeChatTemplate.class, id);
                        try {
                            weChatTemplateDao.deleteById(item.getTemplateId());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
            );
            return Result.ok();
        }
        return Result.error();
    }
}

